<?php
/**
 * Created by PhpStorm.
 * User: boye009
 * Date: 2016/3/11
 * Time: 15:27
 */

namespace Api\Domain;

use Admin\Api\FreightTemplateApi;
use Shop\Api\ProductApi;
use Shop\Api\ProductImageApi;
use Shop\Api\ProductSkuApi;
use Shop\Api\ShoppingCartApi;

class ShoppingCartDomain extends BaseDomain{

    protected $business_code = "shopping";

    protected $allowType = array("json", "rss", "html");

    public function edit(){

        $notes = "应用".$this->client_id."，调用购物车批量修改接口";
        addLog("ShoppingCart/edit",$_GET,$_POST,$notes);

        if(IS_POST){
            $id = $this -> _post('id','','缺少购物车id参数');
            $count = $this -> _post('count','','缺少数量参数');

            $id = explode(',',$id);
            $count = explode(',',$count);

            $res = "";

            foreach($id as $k => $v){

                $map = array(
                    'id' => $v,
                );
                $entity = array(
                    'count' => $count[$k],
                );
                $result = apiCall(ShoppingCartApi::SAVE,array($map,$entity));
                if($result['status']){
                    $res = $res.$v.',';
                }else{
                    $this -> apiReturnErr("发生错误".$result['info']."未完全修改！已修改".$res);
                }

            }
            $this -> apiReturnSuc('修改成功！');

        }

    }

    public function add(){

        $notes = "应用".$this->client_id."，调用购物车添加接口";
        addLog("ShoppingCart/add",$_GET,$_POST,$notes);

        $uid = $this -> _post('uid',0,'用户id缺失');
        $p_id = $this -> _post('p_id',0,'商品id缺失');
        $count = $this -> _post('count',0,'商品数量缺失');
        $psku_id = $this -> _post('psku_id',0,'规格id缺失');

        $store_id = 0;
        $name = "";
        $template_id = 0;
        $weight = 0.0;

        $main_img = "";

        $sku_id = "";
        $sku_desc = "";
        $icon_url = "";
        $price = 0.0;
        $ori_price = 0.0;

        $id = 0;
        $ori_count = 0;

        if($count < 0){
            $this ->apiReturnErr('商品数量有误');
        }

        $map = array(
            'id' => $p_id,
        );

        $result = apiCall(ProductApi::GET_INFO,array($map));
        if($result['status']){
            $store_id = $result['info']['store_id'];
            $name = $result['info']['name'];
            $template_id = $result['info']['template_id'];
            $weight = $result['info']['weight'];
        }else{
            $this -> apiReturnErr($result['info']);
        }

        $map = array(
            'id' => $result['info']['template_id'],
        );
        $result = apiCall(FreightTemplateApi::GET_INFO,array($map));
        if($result['status']){
            if(is_null($result['info'])){
                $this -> apiReturnErr('请联系商家检查商品运费模板');
            }
        }else{
            $this -> apiReturnErr($result['info']);
        }

        $map = array(
            'id' => $psku_id,
        );

        $result = apiCall(ProductSkuApi::GET_INFO,array($map));
        if($result['status']){
            if($result['info']['product_id'] == $p_id){
                $sku_id = $result['info']['sku_id'];
                $sku_desc = $result['info']['sku_desc'];
                $price = $result['info']['price'];
                $ori_price = $result['info']['ori_price'];
                $icon_url = $result['info']['icon_url'];
            }else{
                $this -> apiReturnErr("商品规格有误");
            }

        }else{
            $this -> apiReturnErr($result['info']);
        }

        if($icon_url == ""){
            $map = array(
                'pid' => $p_id,
                'type'=>6015,//主图
            );
            $result = apiCall(ProductImageApi::GET_INFO,array($map));
            if($result['status']){
                $icon_url = $result['info']['img_id'];
            }else{
                $this -> apiReturnErr($result['info']);
            }
        }


        //检查购物车中是否已经存有该商品该规格的信息
        $map = array(
            'uid' => $uid,
            'p_id' => $p_id,
            'psku_id' => $psku_id,
        );
        $result = apiCall(ShoppingCartApi::GET_INFO,array($map));
        if($result['status']){
            if(!is_null($result['info'])){
                $id = $result['info']['id'];
                $ori_count = $result['info']['count'];
            }
        }else{
            $this -> apiReturnErr($result['info']);
        }

        $count = $count + $ori_count;

        $entity = array(
            'uid' => $uid,
            'store_id' => $store_id,
            'p_id' => $p_id,
            'sku_id' => $sku_id,
            'sku_desc' => $sku_desc,
            'icon_url' => $icon_url,
            'count' => $count,
            'name' => $name,
            'express' => 0.0,
            'template_id' => $template_id,
            'price' => $price,
            'ori_price' => $ori_price,
            'psku_id' => $psku_id,
            'weight' => $weight,
            'taxRate' => 0.0,
        );

        if($id == 0){
            $result = apiCall(ShoppingCartApi::ADD,array($entity));
        }else{
            $result = apiCall(ShoppingCartApi::SAVE_BY_ID,array($id,$entity));
        }

        if($result['status']){
            if($id == 0){
                $this -> apiReturnSuc($result['info']);
            }else{
                $this -> apiReturnSuc($id);
            }
        }else{
            $this -> apiReturnErr($result['info']);
        }

    }

    public function count(){

        $notes = "应用".$this->client_id."，调用购物车对应商品数量接口";
        addLog("ShoppingCart/count",$_GET,$_POST,$notes);

        if(IS_POST){

            $psku_id = $this -> _post('psku_id',0,"商品规格id缺失");

            $map = array(
                'id' => $psku_id,
            );

            $result = apiCall(ProductSkuApi::GET_INFO,array($map));

            if($result['status']){
                $pcount = $result['info']['quantity'];
                if($pcount === NULL){
                    $this -> apiReturnErr("规格参数错误");
                }
                $this -> apiReturnSuc($pcount);
            }else{
                $this -> apiReturnErr($result['info']);
            }

        }

    }

    public function query(){

        $notes = "应用".$this->client_id."，调用购物车分页查询接口";
        addLog("ShoppingCart/query",$_GET,$_POST,$notes);

        if(IS_POST){

            $uid = $this -> _post('uid',0,'用户id缺失');
            $page_no = $this -> _post('page_no',0);
            $page_size = $this -> _post('page_size',10);
            $map = array(
                'uid' => $uid,
            );

            $page = array('curpage'=>$page_no,'size'=>$page_size); //分页查询
            $order="store_id desc,update_time desc";
            $result=apiCall(ShoppingCartApi::QUERY,array($map,$page,$order));

            if($result['status']){
                $list = $result['info']['list'];
                if(is_array($list)){
                    foreach($list as &$vo){
                        $vo['icon_url'] = getImageUrl($vo['icon_url']);
                    }
                }
                $this->apiReturnSuc($list);
            }else{
                $this -> apiReturnErr($result['info']);
            }

        }

    }

    public function getInfo(){

        $notes = "应用" . $this->client_id . "，调用单个购物车查询接口";
        addLog("ShoppingCart/getInfo", $_GET, $_POST, $notes);

        $id = $this -> _post('id',0,"缺少购物车id参数");
        $map = array(
            'id' => $id,
        );

        $result=apiCall(ShoppingCartApi::GET_INFO,array($map));
        if($result['status']){
            if($result['info']!=null){

                $result['info']['icon_url'] = getImageUrl($result['info']['icon_url']);

                $this->apiReturnSuc($result['info']);
            }else{
                $this->apiReturnErr("查询失败");
            }

        }else{
            $this->apiReturnErr("查询失败");
        }

    }

    public function delete(){

        $notes = "应用" . $this->client_id . "，调用购物车删除接口";
        addLog("ShoppingCart/delete", $_GET, $_POST, $notes);

        $id = $this -> _post('id',-1,'缺少购物车id参数');

        $id = explode(',',$id);

        $success_ids = "";
        foreach($id as $k => $v){
            $map = array(
                'id'=>$v,
            );
            $result = apiCall(ShoppingCartApi::DELETE,array($map));
            if($result['status']){
                $success_ids = $success_ids.$v.';';
            }else{
                $this -> apiReturnErr("未完全删除成功!".$result['info'].'..已删除成功'.$success_ids);
            }
        }

        $this -> apiReturnSuc('删除成功');

    }

}